package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.domain.dto.EmailDto;
import com.ruoyi.mapper.LendMessageMapper;
import com.ruoyi.mapper.PenaltyMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.Message;
import com.ruoyi.service.IMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息中心Controller
 *
 * @author qyb
 * @date 2023-12-26
 */
@RestController
@RequestMapping("/system/message")
public class MessageController extends BaseController
{
    @Autowired
    private IMessageService messageService;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private LendMessageMapper lendMessageMapper;
    @Autowired
    private PenaltyMapper penaltyMapper;
    /**
     * 查询消息中心列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(Message message)
    {
        startPage();
        List<Message> list = messageService.selectMessageList(message);
        return getDataTable(list);
    }

    /**
     * 导出消息中心列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:export')")
    @Log(title = "消息中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Message message)
    {
        List<Message> list = messageService.selectMessageList(message);
        ExcelUtil<Message> util = new ExcelUtil<Message>(Message.class);
        util.exportExcel(response, list, "消息中心数据");
    }

    /**
     * 获取消息中心详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:message:query')")
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") Long messageId)
    {
        return success(messageService.selectMessageByMessageId(messageId));
    }



    /**
     * 修改消息中心
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @Log(title = "消息中心", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Message message)
    {
        return toAjax(messageService.updateMessage(message));
    }

    /**
     * 删除消息中心
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @Log(title = "消息中心", businessType = BusinessType.DELETE)
	@DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds)
    {
        return toAjax(messageService.deleteMessageByMessageIds(messageIds));
    }
    /**
     *
     */
    @GetMapping("/testValue")
    public AjaxResult send()
    {
        int lendExpiring = Integer.parseInt(sysDictDataMapper.selectValueByType("sys_lend_expiring"));
        List<EmailDto> emaiLDtos = lendMessageMapper.selectDueBookByDay(lendExpiring);
        return AjaxResult.success();
    }


    @GetMapping("/testMessage")
    public AjaxResult sendMessage()
    {
        messageService.sendCallMessage();
        messageService.sendFineMessage();
        return AjaxResult.success();
    }


    /**
     * 发送罚款通知 开启事务
     */
//    @GetMapping("/sendFine")
//    public AjaxResult send()
//    {
//
//        int code = 0;
//        String msg = "未知信息";
//        try {
//            messageService.sendFineMessage();
//                code = 200;
//                msg = "登录成功";
//            } catch (Exception e){
//            msg = e.getMessage();
//            code = 401;
//            }
//        AjaxResult ajaxResult = new AjaxResult(code,msg);
//        return ajaxResult;
//    }

}
